package operations

import (
	"os"

	"gopkg.in/yaml.v2"

	"aicloud.cn/aicloud-operate/pkg/util"
)

var Operations = Operation{}

type Operation struct {
	Ops []Ops `yaml:"ops"`
}

type Ops struct {
	Name  string     `yaml:"name" json:"name"`
	Cname string     `yaml:"cname" json:"cname"`
	Ops   []SingleOp `yaml:"ops" json:"ops"`
}

// SingleOp Name为操作名称；Cname为展示名称
type SingleOp struct {
	Name  string `yaml:"name" json:"name"`
	Cname string `yaml:"cname" json:"cname"`
}

func LoadOpsYaml(operationConfigFile string) {
	_, err := os.Stat(operationConfigFile)
	if err != nil {
		operationConfigFile = "./config/operations/operation.yaml"
	}
	configBytes, err := os.ReadFile(operationConfigFile)
	if err != nil {
		util.GetLogger().Fatalf("load operation config file failed: %v", err)
	}
	if err = yaml.Unmarshal(configBytes, &Operations); err != nil {
		util.GetLogger().Fatalf("unmarshal operation config file failed: %v", err)
	}
}
